package com.yan.demo;

import java.util.Arrays;

// 二分查找
public class Test8 {
    public static void main(String[] args) {
        int[] arr = {3, 12, 24, 36, 55, 68, 75, 88};
        int left = 0, right = arr.length - 1;
        int mid;
        int num = 12;
        while (true) {
            mid = (left + right) / 2;
            if (num > arr[mid]) {
                left = mid + 1;
            } else if (num < arr[mid]) {
                right = mid - 1;
            } else {
                System.out.println(num + "的索引位置为" + mid);
                break;
            }
            if (left > right) {
                System.out.println("没有该元素");
                break;
            }
        }
    }
}
